package com.zprmb.udzt;


import android.app.KeyguardManager;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Color;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import android.os.Bundle;
import android.support.v4.hardware.fingerprint.FingerprintManagerCompat;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.zprmb.udzt.activity.PayActivity2;
import com.zprmb.udzt.http.AjaxParams;
import com.zprmb.udzt.util.Constant;
import com.zprmb.udzt.util.CustomDialog;
import com.zprmb.udzt.util.CustomDialog_view;
import com.zprmb.udzt.util.FingerSetting;
import com.zprmb.udzt.util.FingerprintUtil;
import com.zprmb.udzt.util.GuideAlertDialog;
import com.zprmb.udzt.util.GuideAlertDialog.OnSureClickListener;
import com.zprmb.udzt.util.LogUtils;
import com.zprmb.udzt.util.NumUtil;
import com.zprmb.udzt.util.Regular;
import com.zprmb.udzt.util.ToastUtil;
import com.zprmb.udzt.util.VolleryUtil;
import com.zprmb.udzt.util.VoteCalculator;
import com.zprmb.udzt.view.AddAndSubView;
import com.zprmb.udzt.view.AddAndSubView.OnNumChangeListener;

import org.json.JSONObject;

import java.text.DecimalFormat;

import static com.zprmb.udzt.R.id.add_subview;

public class V6iCoupActivity extends BaseActivity implements OnClickListener, OnSureClickListener {
    private AddAndSubView addAndSubView;
    private RelativeLayout rl_back;
    private TextView tv_title;
    private TextView tv_availableAmount, tv_dexterityyue, tv_product_remainamount;
    private TextView tv_min_increaseNum;
    private String name, type, loanid;
    private TextView check_mAgreement;
    private RelativeLayout rl_use_coupon;
    private String reward_id;//礼券id
    private DecimalFormat df = new DecimalFormat("###0.00");
    private String red_type = ""; //红包类型
    private int ischange = 0;
    private Double availableGiftAmount = 0.0;//用户红包总量
    private Double coupon_rate = 0.0, red_rate = 0.0;//加息券利率 红包利率
    private TextView coupon_num;//加息券所剩张数
    private TextView coupon_name;//加息券名字
    private int month;
    private Double rate;
    private CheckBox check_dexterity;
    private TextView tv_add_money;
    private boolean use_status = false;
    private String flagFresh;
    private SharedPreferences sha;
    private Double availableAmount = 0.0;
    private TextView tv_product_shouyi;
    int minAmount = 0;
    private boolean finger_Count = false;
    private ImageView img_ck_status;
    private LinearLayout li_tishi;
    private DecimalFormat df2 = new DecimalFormat("###0");
    private Double maxAmount;
    int remainAmount = 0;
    private boolean payPass = true;
    private Double curAmount = 0.0, maxcurAmount = 0.0;
    private boolean nameauth = true, islogin = true;
    protected int increaseNum;
    private CheckBox check;
    private boolean ishuo;
    protected boolean tishi_coupon = true;
    protected boolean resultcoupon = false;
    private TextView tv_tishi;
    private TextView tv_mAgreement;
    private Button btn_invest;
    private boolean flagred;
    private RelativeLayout rl_dexterity;
    private int coupon_max, coupon_min;
    private int useFundAmountInvestFlag = 1;//特权时间
    private String firstType = "";
    private CustomDialog_view.Builder builder;
    private CustomDialog.Builder mDialog;
    String firstamount = "";
    private GuideAlertDialog mAlertDialog;

    //	private Double proportion;//项目利息

    private TextView text_coupon;
    //抵扣的布局
    private LinearLayout layout_dikou;
    //抵扣金额
    private TextView text_dikou;
    //实际金额
    private TextView text_shiji;
    //title

    private TextView text_title;
    //礼券布局
    private LinearLayout layout_liquan;
    int intentpaymoney = 0;
    KeyguardManager keyguardManager = (KeyguardManager) BbtApplication.getContext().getSystemService(BbtApplication.getContext().KEYGUARD_SERVICE);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.invest_activity);
//        AndroidSetView.assistActivity(findViewById(R.id.li_all));
//        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

  /*  public boolean checkDeviceHasNavigationBar(){
        boolean hasNavigationBar = false;Resources rs = getResources();
        int id = rs.getIdentifier("config_showNavigationBar","bool","android");
        if (id > 0){ hasNavigationBar = rs.getBoolean(id); } try {
            Class*/
        setImmersionStatus();
        setContentInview(V6iCoupActivity.this).setVisibility(View.INVISIBLE);
        initIntent();
        mAlertDialog = new GuideAlertDialog(this, this);
        tv_title = (TextView) findViewById(R.id.tv_title);
        rl_back = (RelativeLayout) findViewById(R.id.rl_back);
        text_coupon = (TextView) findViewById(R.id.text_coupon);
        initView();
        rl_back.setOnClickListener(this);
        layout_dikou = (LinearLayout) findViewById(R.id.layout_dikou);
        //抵扣金额
        text_dikou = (TextView) findViewById(R.id.text_dikou);
        //实际金额
        text_shiji = (TextView) findViewById(R.id.text_shiji);
        layout_dikou.setVisibility(View.GONE);
        //折扣券的产品
        text_title = (TextView) findViewById(R.id.text_title);
        text_title.setVisibility(View.GONE);
        inintData(1);
        layout_liquan = (LinearLayout) findViewById(R.id.layout_liquan);
//		userinfo();
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
//        inintData(0);
    }

    /*  hpay/v3/h5recharge
      String amount 充值金额
      String path 充值类型
      String card_no 银行卡号
      bank_name 银行编号
      String token
      String loan_id 产品ID
      String type 产品类型
      String investment_Amount 投资金额
      String coupon_id 道具id
      String coupon_type 道具类型*/
    private void myconmment() {
        if (useFundAmountInvestFlag == 0) {

            if (addAndSubView.getNum() != 0 && addAndSubView.getNum() > curAmount && islogin && nameauth) {
                if (curAmount == 0) {

                    li_tishi.setVisibility(View.VISIBLE);
                    tv_tishi.setText("特权时间过后即可投资");
                } else {
                    if (addAndSubView.getNum() != 0 && addAndSubView.getNum() > maxcurAmount) {
                        li_tishi.setVisibility(View.VISIBLE);
                        tv_tishi.setText("智投宝单笔转投金额不能超过上限" + (df2.format(maxcurAmount / 10000)) + "万!");

                    } else {
                        li_tishi.setVisibility(View.VISIBLE);
                        if (addAndSubView.getNum() != 0 && addAndSubView.getNum() > curAmount) {
                            tv_tishi.setText("智投宝余额不足");

                        } else {

                            li_tishi.setVisibility(View.GONE);
                        }
                    }

                }
            } else {
                if (addAndSubView.getNum() != 0 && addAndSubView.getNum() > maxcurAmount) {
                    li_tishi.setVisibility(View.VISIBLE);
                    tv_tishi.setText("智投宝单笔转投金额不能超过上限" + (df2.format(maxcurAmount / 10000)) + "万!");

                } else {
                    li_tishi.setVisibility(View.VISIBLE);
                    if (addAndSubView.getNum() != 0 && addAndSubView.getNum() > curAmount) {
                        tv_tishi.setText("智投宝余额不足");
                    } else {
                        if (increaseNum != 0) {


                            if (minAmount > remainAmount) {
//								if((Integer.valueOf(et_money.getText().toString())!=remainAmount)){
//									ToastUtil.showCenterToast(DetailsActivity.this,
//											"投资金额的增量要为" + increaseNum + "的倍数");
//									return;
//								}
                                li_tishi.setVisibility(View.GONE);
                            } else {
                                if ((addAndSubView.getNum() - minAmount)
                                        % increaseNum != 0) {
                                    li_tishi.setVisibility(View.VISIBLE);
                                    tv_tishi.setText("投资金额的增量要为" + increaseNum + "的倍数");
//									ToastUtil.showCenterToast(V6investActivity.this,
//											"投资金额的增量要为" + increaseNum + "的倍数");

                                } else {
                                    li_tishi.setVisibility(View.GONE);
                                }
                            }
                        }
                    }
                }
            }
            if (resultcoupon) {

                if (addAndSubView.getNum() > coupon_max || addAndSubView.getNum() < coupon_min) {
                    li_tishi.setVisibility(View.VISIBLE);
                    tv_tishi.setText("投资金额不在福利限投金额内,重新选择福利或重新输入投资金额");
                    li_tishi.postDelayed(new Runnable() {

                        @Override
                        public void run() {
                            // TODO Auto-generated method stub
                            li_tishi.setVisibility(View.GONE);
                            changeStatus();
                        }
                    }, 2000);
                }
            }
            changeStatus();

        } else {
            if (ishuo) {
                if (addAndSubView.getNum() != 0
                        && ishuo && addAndSubView.getNum() > (curAmount + availableAmount)
                        && islogin && nameauth) {

                    if (addAndSubView.getNum() != 0 && (addAndSubView.getNum() - availableAmount) > maxcurAmount && curAmount >= maxcurAmount) {
                        li_tishi.setVisibility(View.VISIBLE);
                        tv_tishi.setText("智投宝单笔转投金额不能超过上限" + (df2.format(maxcurAmount / 10000)) + "万!");

                    } else {
                        li_tishi.setVisibility(View.VISIBLE);
                        tv_tishi.setText("账户可用余额不足,请充值!");

//					    	 investComment();
                    }

                } else {
                    if (addAndSubView.getNum() != 0 && (addAndSubView.getNum() - availableAmount) > maxcurAmount) {
                        li_tishi.setVisibility(View.VISIBLE);
                        tv_tishi.setText("智投宝单笔转投金额不能超过上限" + (df2.format(maxcurAmount / 10000)) + "万!");
                    } else {
                        investComment();
                    }

                }
            } else {
                if (addAndSubView.getNum() != 0
                        && availableAmount < addAndSubView.getNum() && (ishuo == false)
                        && islogin && nameauth) {

                    if (curAmount > 0) {
                        li_tishi.setVisibility(View.VISIBLE);
                        tv_tishi.setText("账户可用余额不足，请充值！");
//							Log.e("提示", "账户可用余额不足，请充值或选择智投宝支付！");

                    } else {
                        li_tishi.setVisibility(View.VISIBLE);
                        tv_tishi.setText("账户可用余额不足,请充值!");

                    }
//						investComment();
                } else {
                    investComment();


                }
            }
            if (resultcoupon) {
                Log.e("max", coupon_max + "");
                Log.e("min", coupon_min + "");
                if (addAndSubView.getNum() > coupon_max || addAndSubView.getNum() < coupon_min) {
                    li_tishi.setVisibility(View.VISIBLE);
                    tv_tishi.setText("投资金额不在福利限投金额内,重新选择福利或重新输入投资金额");
                    li_tishi.postDelayed(new Runnable() {

                        @Override
                        public void run() {
                            // TODO Auto-generated method stub
                            li_tishi.setVisibility(View.GONE);
                            changeStatus();
                        }
                    }, 2000);
                }
            }
            changeStatus();
        }
    }

    private void changeStatus() {
        if (li_tishi.getVisibility() == View.VISIBLE) {
            tv_min_increaseNum.setVisibility(View.INVISIBLE);
        } else {
            tv_min_increaseNum.setVisibility(View.VISIBLE);
        }
    }

    private void investComment() {
        if (increaseNum != 0) {


            if (minAmount > remainAmount) {
//				if((Integer.valueOf(et_money.getText().toString())!=remainAmount)){
//					ToastUtil.showCenterToast(DetailsActivity.this,
//							"投资金额的增量要为" + increaseNum + "的倍数");
//					return;
//				}
                li_tishi.setVisibility(View.GONE);
            } else {
                if ((addAndSubView.getNum() - minAmount)
                        % increaseNum != 0) {
                    li_tishi.setVisibility(View.VISIBLE);
                    tv_tishi.setText("投资金额的增量要为" + increaseNum + "的倍数");
//					ToastUtil.showCenterToast(V6investActivity.this,
//							"投资金额的增量要为" + increaseNum + "的倍数");

                } else {
                    li_tishi.setVisibility(View.GONE);
                }
            }
        }
        if (maxAmount != 0) {
            if (addAndSubView.getNum() > maxAmount) {
                li_tishi.setVisibility(View.VISIBLE);
//                tv_tishi.setText("最大投资金额为" + maxAmount + "元");
//                tv_tishi.setText("最大投资金额为" + maxAmount + "元");
                tv_tishi.setText("投资金额不能大于" + maxAmount.intValue() + " !");
            } else {
                if ((addAndSubView.getNum() - minAmount)
                        % increaseNum != 0) {

                } else {

                    li_tishi.setVisibility(View.GONE);
                }
            }
        }
        if (remainAmount != 0) {
            if (addAndSubView.getNum() > remainAmount) {

                li_tishi.setVisibility(View.VISIBLE);
                tv_tishi.setText("投资金额不能大于剩余可投金额！");
            } else {
                if ((addAndSubView.getNum() - minAmount)
                        % increaseNum != 0 || addAndSubView.getNum() > maxAmount) {

                } else {

                    li_tishi.setVisibility(View.GONE);
                }
            }
        }
    }

    private void inintData(final int creat) {
        // TODO Auto-generated method stub
        VolleryUtil volleryUtil = new VolleryUtil() {

            @Override
            public void onUpDate(JSONObject jsonObject) {
                // TODO Auto-generated method stub
                setContentInview(V6iCoupActivity.this).setVisibility(View.VISIBLE);
                if (jsonObject.optInt("success") == 1) {
//					progressLayout.showContent();
                    LogUtils.e(V6iCoupActivity.this, "定期投资页面" + jsonObject.toString());
                    JSONObject json1 = jsonObject.optJSONObject("data");

                    if (json1 != null) {
                        JSONObject user = json1.optJSONObject("user");
                        JSONObject jsoncoupon = json1.optJSONObject("couponInfo");
                        rate = json1.optDouble("rate");
                        minAmount = json1.optInt("minAmount");
                        remainAmount = json1.optInt("remainAmount");
                        maxAmount = json1.optDouble("maxAmount");
                        increaseNum = json1.optInt("increaseNum");
                        flagFresh = json1.optString("flagFresh");
                        if (user != null) {
                            check_dexterity.setEnabled(true);
                            rl_dexterity.setEnabled(true);
                            islogin = true;
                            d3 = user.optDouble("availableAmount");
                            tv_availableAmount.setText(Regular.getDecimalFormatTwo(user.optDouble("availableAmount")));
                            tv_dexterityyue.setText(Regular.getDecimalFormatTwo(user.optDouble("curAmount")));
//							availableGiftAmount=user.optDouble("availableGiftAmount ");
                            availableAmount = user.optDouble("availableAmount");
                            curAmount = user.optDouble("curAmount");
                            maxcurAmount = user.optDouble("maxTransferInvestAmount");
                            useFundAmountInvestFlag = user.optInt("useFundAmountInvestFlag");
                        } else {
                            check_dexterity.setEnabled(false);
                            rl_dexterity.setEnabled(false);
                            islogin = false;
                            tv_availableAmount.setText("0.00");
                            tv_dexterityyue.setText("0.00");
                            coupon_name.setText("登录查看");
                            coupon_num.setText("");
                        }
                        month = json1.optInt("days");
                        if (creat == 1) {
                            addAndSubView.setNum(json1.optInt("minAmount"));
                        }
                        if (jsoncoupon != null) {
                            if (ischange == 2 && !sha.getString("check", "").equals("")) {

                            } else {
                                coupon_name.setTextColor(Color.parseColor("#64A6FE"));
                                coupon_name.setText(jsoncoupon.optString("couponMsg"));
                                if (!coupon_name.getText().toString().equals("登录查看")) {
                                    red_type = "";
                                    resultcoupon = false;
                                    coupon_num.setText(jsoncoupon.optString("couponCount") + "张");
                                }
                            }
                        }

                        tv_product_remainamount.setText(NumUtil.getQiantwo(json1.optString("remainAmount")) + "元");

                        tv_min_increaseNum.setText(df2.format(json1.optDouble("minAmount")) + "元起投," + json1.optString("increaseNum") + "元倍数递增");

//						addAndSubView.setMinnum(json1.optInt("minAmount"));
                        addAndSubView.setincreaseNum(json1.optInt("increaseNum"));
                    }
                }
            }

            @Override
            public void onError() {
                // TODO Auto-generated method stub

            }
        };
        volleryUtil.setHeaders("token", getToken());
        volleryUtil.setRequestMethod(0);
        volleryUtil.setShow(true);
        volleryUtil.execute(V6iCoupActivity.this, Constant.URL + "/api/v6/render/assetDetail/" + type + "/" + loanid, "");
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (sha != null) {
                Editor editor = sha.edit();
                editor.clear();
                editor.commit();
            }
        }

        return super.onKeyDown(keyCode, event);
    }

    private void initView() {
        // TODO Auto-generated method stub
        addAndSubView = (AddAndSubView) findViewById(add_subview);
        addAndSubView.setEditTextLayoutWidth(800);
        if (getScreenWidth() > 1080) {
            addAndSubView.setButtonLayoutParm(100, 100);
        } else {
            addAndSubView.setButtonLayoutParm(60, 60);
        }
        check = (CheckBox) findViewById(R.id.check);
        check_mAgreement = (TextView) findViewById(R.id.check_mAgreement);
        rl_dexterity = (RelativeLayout) findViewById(R.id.rl_dexterity);
        addAndSubView.setTextSize(18);
        li_tishi = (LinearLayout) findViewById(R.id.li_tishi);
        tv_mAgreement = (TextView) findViewById(R.id.tv_mAgreement);
        sha = getSharedPreferences("coupon", MODE_PRIVATE);
        img_ck_status = (ImageView) findViewById(R.id.ck_status);
        tv_product_shouyi = (TextView) findViewById(R.id.tv_product_shouyi);
        tv_tishi = (TextView) findViewById(R.id.tv_tishi);
        addAndSubView.setButtonBgResource(R.drawable.jiahao, R.drawable.jianhao);
        tv_availableAmount = (TextView) findViewById(R.id.tv_availableAmount);
        tv_dexterityyue = (TextView) findViewById(R.id.tv_dexterityyue);
        check_dexterity = (CheckBox) findViewById(R.id.check_dexterity);
        tv_product_remainamount = (TextView) findViewById(R.id.tv_product_remainamount);
        tv_min_increaseNum = (TextView) findViewById(R.id.tv_min_increaseNum);
        rl_use_coupon = (RelativeLayout) findViewById(R.id.rl_use_coupon);
        coupon_num = (TextView) findViewById(R.id.coupon_num);
        coupon_name = (TextView) findViewById(R.id.v6_coupon_name);
        tv_add_money = (TextView) findViewById(R.id.tv_add_money);
        btn_invest = (Button) findViewById(R.id.btn_invest);
        check_mAgreement.setOnClickListener(this);
        tv_mAgreement.setOnClickListener(this);
        btn_invest.setOnClickListener(this);
        tv_add_money.setOnClickListener(this);
        rl_use_coupon.setOnClickListener(this);
        rl_dexterity.setOnClickListener(this);
        check.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                // TODO Auto-generated method stub
                if (isChecked) {
                    btn_invest.setEnabled(true);
                    btn_invest.setBackgroundResource(R.drawable.v6button);
                } else {
                    btn_invest.setEnabled(false);
                    btn_invest.setBackgroundResource(R.drawable.btn_unclick);
                }
            }
        });
        check_dexterity.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                // TODO Auto-generated method stub
                if (isChecked) {
                    img_ck_status.setVisibility(View.VISIBLE);
                    check_dexterity.setChecked(true);
                } else {
                    img_ck_status.setVisibility(View.GONE);
                    check_dexterity.setChecked(false);
                }
                ishuo = isChecked;
                myconmment();
            }
        });
        addAndSubView.setOnNumChangeListener(new OnNumChangeListener() {

            @Override
            public void onNumChange(View view, int num) {
                Log.e("sha", sha.getString("check", "22"));
                // TODO Auto-generated method stub
                myconmment();
                product_ShouYi();
                if (ischange == 2) {
                    if (use_status) {

                        interest();
                    } else {
                        incoupon();
                    }
                } else {

                }

            }
        });
        tv_title.setText(name);
    }

    Double d3 = 0.0;

    private void setImmersionStatus() {
        if (VERSION.SDK_INT >= VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
    }

    private void initIntent() {
        // TODO Auto-generated method stub
        name = getIntent().getStringExtra("name");
        type = getIntent().getStringExtra("type");
        loanid = getIntent().getStringExtra("id");
    }

    private void userinfo() {
        VolleryUtil vollery = new VolleryUtil() {

            @Override
            public void onUpDate(JSONObject jsonObject) {
                // TODO Auto-generated method stub
                if (jsonObject.optInt("success") == 1) {
                    JSONObject json1 = jsonObject.optJSONObject("data");
                    if (json1 != null) {
                        JSONObject jsoncoupon = jsonObject.optJSONObject("couponInfo");
                        d3 = json1.optDouble("availableAmount");
                        tv_availableAmount.setText(Regular.getDecimalFormatTwo(json1.optDouble("availableAmount")));
                        tv_dexterityyue.setText(Regular.getDecimalFormatTwo(json1.optDouble("ltbAmount")));
                        tv_product_remainamount.setText(Regular.getDecimalFormatTwo(json1.optDouble("remainAmount")));
                        tv_min_increaseNum.setText(json1.optString("minAmount") + "元起投" + json1.optString("increaseNum") + "元倍数递增");
                        month = json1.optInt("days");
                        if (jsoncoupon != null) {
                            coupon_num.setText(jsoncoupon.optString("couponCount"));
                            coupon_name.setText(jsoncoupon.optString("couponMsg"));
                        }
                    }
                }
                LogUtils.e(V6iCoupActivity.this, jsonObject.toString());
//				ToJSON.initData2(jsonObject.toString(),"userinfo" );
            }

            @Override
            public void onError() {
                // TODO Auto-generated method stub

            }
        };
        vollery.setRequestMethod(0);
        vollery.setHeaders("token", getToken());

        vollery.execute(this, Constant.URL + "/api/v5/user/userInfo", "");
    }

    private void interest() {
        Log.e("计息", "开始计算");
        if (!red_type.equals("")) {
            if (addAndSubView.getNum() != 0) {
                if (red_type.equals("PERCENTAGE")) {
                    double redpage = addAndSubView.getNum() * (0.005);
                    if (redpage > availableGiftAmount) {
                        redpage = availableGiftAmount;
                    }

                } else {
                    Log.e("num", addAndSubView.getNum() + "----" + month + "---" + addAndSubView.getNum() * (red_rate / 100) / 365 * month + "   " + red_rate);
                    double redpage = addAndSubView.getNum() * (red_rate / 100) / 365 * month;
                    if (redpage > availableGiftAmount) {
                        redpage = availableGiftAmount;

                    }
                    Log.e("redpage", redpage + "" + availableGiftAmount);
                    coupon_num.setText(Regular.setSpannablecolor("收益:" + df.format(redpage), "#cccccc", 0, 3)
                            + "元");
                    Log.e("shouyi", coupon_num.getText().toString());

                }
            }
        }

    }

    String cardId, getIntentmoney, addtype, myflag = "3";
    String shijie = "0";
    String dikou = "0";
    String touzijine = "0";

    @Override
    protected void onActivityResult(int arg0, int arg1, Intent intent) {
        // TODO Auto-generated method stubx
        super.onActivityResult(arg0, arg1, intent);
        if (intent != null) {
            if (arg1 == 2) {
                addtype = intent.getStringExtra("add_type");
                myflag = intent.getStringExtra("my_flag");
                cardId = intent.getStringExtra("cardId");
                ischange = arg1;

                if (myflag.equals("1")) {
                    flagred = true;
                    resultcoupon = false;
                    use_status = true;
                    tishi_coupon = false;
                    layout_dikou.setVisibility(View.GONE);
                    reward_id = intent.getStringExtra("id");
                    red_rate = Double.parseDouble(intent.getStringExtra("rate"));
                    red_type = intent.getStringExtra("red_type");
                    availableGiftAmount = Double.parseDouble(intent.getStringExtra("availableGiftAmount"));
                    Log.e("intent", availableGiftAmount + "");
                    Log.e("intentname", intent.getStringExtra("name") + "");
                    Log.e("intentrate", intent.getStringExtra("rate") + "");
                    coupon_name.setTextColor(Color.parseColor("#cccccc"));
                    coupon_name.setText(intent.getStringExtra("name"));
                    text_coupon.setText("可用礼券");
                    coupon_num.setVisibility(View.VISIBLE);
                    text_title.setVisibility(View.GONE);
                    layout_dikou.setVisibility(View.GONE);
                    layout_liquan.setVisibility(View.GONE);
                    interest();
                    touzijine = intent.getStringExtra("touzijine");

                    text_title.setVisibility(View.GONE);
                } else if (myflag.equals("2")) {
                    layout_dikou.setVisibility(View.VISIBLE);
                    layout_liquan.setVisibility(View.VISIBLE);

                    flagred = false;
                    resultcoupon = true;
                    use_status = false;
                    tishi_coupon = false;
                    getIntentmoney = intent.getStringExtra("getMoney");
                    reward_id = intent.getStringExtra("id");
                    red_type = intent.getStringExtra("red_type");
                    coupon_rate = Double.parseDouble(intent.getStringExtra("rate"));

                    coupon_name.setTextColor(Color.parseColor("#6bacfd"));
                    text_coupon.setText("可用福利");
                    coupon_name.setText(intent.getStringExtra("name"));
                    coupon_max = intent.getIntExtra("maxAmount", 0);
                    coupon_min = intent.getIntExtra("minAmount", 0);
                    Log.e("red_type", red_type + "");
                    Log.e("reward_id", reward_id + "");
//                    text_shiji.setText(String.valueOf(intentpaymoney));
                    coupon_num.setVisibility(View.GONE);
                    text_dikou.setText(intent.getStringExtra("zhekouMoney"));
                    dikou = intent.getStringExtra("zhekouMoney");
                    int c;

                    int a = addAndSubView.getNum();
                    int b = Integer.valueOf(intent.getStringExtra("zhekouMoney"));
                    c = a - b;
                    shijie = String.valueOf(c);
                    text_shiji.setText(shijie);
                    text_title.setText(intent.getStringExtra("zhekouMoney") + intent.getStringExtra("intentTitle"));

                    touzijine = intent.getStringExtra("touzijine");

                    incoupon();
                    text_title.setVisibility(View.VISIBLE);
                } else {
                    flagred = false;
                    resultcoupon = true;
                    use_status = false;
                    tishi_coupon = false;
                    layout_dikou.setVisibility(View.VISIBLE);
                    reward_id = intent.getStringExtra("id");
                    red_type = intent.getStringExtra("red_type");
                    text_coupon.setText("可用礼券");
                    coupon_num.setVisibility(View.VISIBLE);
                    coupon_rate = Double.parseDouble(intent.getStringExtra("rate"));
                    coupon_name.setTextColor(Color.parseColor("#cccccc"));
                    coupon_name.setText(intent.getStringExtra("name"));
                    coupon_max = intent.getIntExtra("maxAmount", 0);
                    coupon_min = intent.getIntExtra("minAmount", 0);
                    incoupon();
                    text_title.setVisibility(View.VISIBLE);
                    text_title.setVisibility(View.GONE);
                    layout_dikou.setVisibility(View.GONE);
                    touzijine = intent.getStringExtra("touzijine");
                }

            } else if (arg1 == 1) {
                ischange = arg1;
                flagred = false;
                reward_id = "";
                red_type = "";
                coupon_rate = 0.0;
                use_status = true;
                tishi_coupon = true;
                resultcoupon = false;
                coupon_num.setVisibility(View.VISIBLE);
//                rl_use_coupon.setVisibility(View.GONE);
                layout_liquan.setVisibility(View.GONE);
                inintData(0);
            }
            if (arg1 == 5) {
                setResult(5, intent);
                finish();
            }

        }
    }

    private void checkUserInfo() {
        VolleryUtil volleryUtil = new VolleryUtil() {

            @Override
            public void onUpDate(JSONObject jsonObject) {
                // TODO Auto-generated method stub
                LogUtils.e(V6iCoupActivity.this, jsonObject.toString());
                JSONObject json = jsonObject.optJSONObject("data");
                if ("false".equals(json.optString("bindCard"))) {
                    mAlertDialog.show();
                    mAlertDialog.setText("请绑定银行卡");
                    mAlertDialog.setTitle("提示");
                    mAlertDialog.setType("bindcard");
                    mAlertDialog.setButtonCancel("稍后");
                    mAlertDialog.setButtonsure("去绑卡");

                    return;
                }
                if ("false".equals(json.optString("payPass"))) {
                    mAlertDialog.show();
                    mAlertDialog.setText("请设置交易密码");
                    mAlertDialog.setTitle("提示");
                    mAlertDialog.setType("password");
                    mAlertDialog.setButtonCancel("稍后");
                    mAlertDialog.setButtonsure("去设置交易密码");
                    return;
                }
                if ("false".equals(json.optString("isRiskAssessment"))) {
                    mAlertDialog.show();
                    mAlertDialog.setText("请进行风险测评");
                    mAlertDialog.setTitle("提示");
                    mAlertDialog.setType("ceping");
                    mAlertDialog.setButtonCancel("稍后");
                    mAlertDialog.setButtonsure("去风险测评");
                    return;
                }
                if (getInvestNum() == 0 && getisFingerPay() == 0) {
                    showDialog();


                } else if (getisFingerPay() == 1) {
                    if (coupon_name.getText().toString().equals("未使用") && tishi_coupon) {
                        tishi_coupon = false;
                        li_tishi.setVisibility(View.VISIBLE);
//                        tv_tishi.setText("亲，您还有未使用的福利哦!");
//                        changeStatus();

                        if (addAndSubView.getNum() != 0 && addAndSubView.getNum() > d3.intValue()) {
//                                                tv_tishi.setText("");
//                        Toast.makeText(V6iCoupActivity.this,"账户可用余额不足,请充值",Toast.LENGTH_SHORT).show();
                            tv_tishi.setText("账户可用余额不足,请充值!");
                            ToastUtil.showCenterToast(V6iCoupActivity.this,
                                    "账户可用余额不足,请充值!");
                            tishi_coupon = true;
                        } else if (addAndSubView.getNum() < minAmount) {
                            ToastUtil.showCenterToast(V6iCoupActivity.this,
                                    "投资金额不能小于最低起投金额");
                            tv_tishi.setText("投资金额不能小于最低起投金额");
                            tishi_coupon = true;
                        } else if (addAndSubView.getNum() > maxAmount) {

                            ToastUtil.showCenterToast(V6iCoupActivity.this,
                                    "投资金额不能大于" + maxAmount.intValue() + "!");
                            tv_tishi.setText("投资金额不能大于" + maxAmount.intValue() + "!");
                            li_tishi.setVisibility(View.VISIBLE);
                            tishi_coupon = true;
                            return;
                        } else if ((addAndSubView.getNum() - minAmount)
                                % increaseNum != 0) {
                            li_tishi.setVisibility(View.VISIBLE);
                            tv_tishi.setText("投资金额的增量要为" + increaseNum + "的倍数");
                            ToastUtil.showCenterToast(V6iCoupActivity.this,
                                    "投资金额的增量要为" + increaseNum + "的倍数");
                            tishi_coupon = true;
                            return;
                        } else {
                            tv_tishi.setText("亲，您还有未使用的福利哦!");
                        }
                        return;

                    } else {
//                        Toast.makeText(V6iCoupActivity.this, curAmount+"", Toast.LENGTH_SHORT).show();


                        if (addAndSubView.getNum() != 0 && addAndSubView.getNum() > Double.valueOf(tv_availableAmount.getText().toString()).intValue()) {
//                                                tv_tishi.setText("");

                            ToastUtil.showCenterToast(V6iCoupActivity.this,
                                    "账户可用余额不足,请充值!");
                            li_tishi.setVisibility(View.VISIBLE);
                            tv_tishi.setText("账户可用余额不足,请充值!");
                            tishi_coupon = true;
                        } else if (addAndSubView.getNum() < minAmount) {
                            ToastUtil.showCenterToast(V6iCoupActivity.this,
                                    "投资金额不能小于最低起投金额");
                            tv_tishi.setText("投资金额不能小于最低起投金额");
                            li_tishi.setVisibility(View.VISIBLE);
                            tishi_coupon = true;
                            return;
                        } else if (addAndSubView.getNum() > maxAmount) {
                          /*  ToastUtil.showCenterToast(V6iCoupActivity.this,
                                    "投资额不能小于最低起投金额");
                            tv_tishi.setText("投资额不能小于最低起投金额");*/
                            ToastUtil.showCenterToast(V6iCoupActivity.this,
                                    "投资金额不能大于" + maxAmount.intValue() + "!");
                            li_tishi.setVisibility(View.VISIBLE);
                            tishi_coupon = true;
                            tv_tishi.setText("投资金额不能大于" + maxAmount.intValue() + "!");
                            return;
                        } else if ((addAndSubView.getNum() - minAmount)
                                % increaseNum != 0) {
                            li_tishi.setVisibility(View.VISIBLE);
                            tishi_coupon = true;
                            tv_tishi.setText("投资金额的增量要为" + increaseNum + "的倍数");
                            ToastUtil.showCenterToast(V6iCoupActivity.this,
                                    "投资金额的增量要为" + increaseNum + "的倍数");
                            return;
                        } else if (coupon_name.getText().toString().equals("未使用") && tishi_coupon) {
                            tishi_coupon = false;
                            li_tishi.setVisibility(View.VISIBLE);
                            tv_tishi.setText("亲，您还有未使用的福利哦!");
                            return;
                        } else {
                            onFingerprintClick(1);
                        }
//                        tv_availableAmount.setText(Regular.getDecimalFormatTwo(json1.optDouble("availableAmount")));
                    }


                } else {

                    verityUser();


                }


            }

            @Override
            public void onError() {
                // TODO Auto-generated method stub

            }
        };
        volleryUtil.setHeaders("token", getToken());
        volleryUtil.setRequestMethod(0);
        volleryUtil.setShow(true);
        volleryUtil.execute(V6iCoupActivity.this, Constant.URL + "/apiAccount/checkAuthentication", "");
    }

    private void verityUser() {
        VolleryUtil vollery = new VolleryUtil() {


            @Override
            public void onUpDate(JSONObject jsonObject) {
                // TODO Auto-generated method stub
                if (jsonObject.optInt("success") == 1) {
                    Log.e("userinfo", jsonObject.toString());
                    JSONObject json = jsonObject.optJSONObject("data");
//					availableGiftAmount = json.optDouble("availableGiftAmount");
                    payPass = json.optBoolean("payPass");
                    Intent intent = null;

                    if (json.optString("bankMobileStatus") != null
                            && !json.optString("bankMobileStatus").equals("")
                            && json.optString("bankMobileStatus").equals(
                            "WAIT_CHECK")) {

                    }
                    if (!json.optBoolean("isBindCard")) {
                        mAlertDialog.show();
                        mAlertDialog.setText("请绑定银行卡");
                        mAlertDialog.setTitle("提示");
                        mAlertDialog.setType("bindcard");
                        mAlertDialog.setButtonCancel("稍后");
                        mAlertDialog.setButtonsure("去绑卡");

                        return;
                    }

                    if ("NOTHING".equals(json.optString("riskKey"))) {
                        mAlertDialog.show();
                        mAlertDialog.setText("请进行风险测评");
                        mAlertDialog.setTitle("提示");
                        mAlertDialog.setType("ceping");
                        mAlertDialog.setButtonCancel("稍后");
                        mAlertDialog.setButtonsure("去风险测评");
                        return;
                    }

                    if (minAmount > remainAmount) {
//
                    } else {
                        if (addAndSubView.getNum() < minAmount) {
                            ToastUtil.showCenterToast(V6iCoupActivity.this,
                                    "投资金额不能小于最低起投金额");
                            tv_tishi.setText("投资金额不能小于最低起投金额");
                            li_tishi.setVisibility(View.VISIBLE);
                            tishi_coupon = true;
                            return;
                        }
                    }


                    if (maxAmount != 0) {
                        if (addAndSubView.getNum() > maxAmount) {

                            if (addAndSubView.getNum() != 0 && addAndSubView.getNum() > d3.intValue()) {
//                                                tv_tishi.setText("");
                                Toast.makeText(V6iCoupActivity.this, "账户可用余额不足,请充值", Toast.LENGTH_SHORT).show();
                                tv_tishi.setText("账户可用余额不足,请充值!");
                                tishi_coupon = true;
                                li_tishi.setVisibility(View.VISIBLE);
                            } else if (addAndSubView.getNum() < minAmount) {
                                Toast.makeText(V6iCoupActivity.this, "投资金额不能小于最低起投金额", Toast.LENGTH_SHORT).show();
                                tv_tishi.setText("投资金额不能小于最低起投金额");
                                tishi_coupon = true;
                                li_tishi.setVisibility(View.VISIBLE);
                            } else if (addAndSubView.getNum() > maxAmount) {

                                ToastUtil.showCenterToast(V6iCoupActivity.this,
                                        "投资金额不能大于" + maxAmount.intValue() + "!");
                                tv_tishi.setText("投资金额不能大于" + maxAmount.intValue() + "!");
                                li_tishi.setVisibility(View.VISIBLE);
                                tishi_coupon = true;
                                return;
                            } else {
                                tv_tishi.setText("亲，您还有未使用的福利哦!");
                                li_tishi.setVisibility(View.VISIBLE);
                            }

                        }
                    }
                    if (remainAmount != 0) {
                        if (addAndSubView.getNum() > remainAmount) {
                            tv_tishi.setText("投资金额不能大于剩余可投金额！");
                            li_tishi.setVisibility(View.VISIBLE);
                            ToastUtil.showCenterToast(V6iCoupActivity.this,
                                    "投资金额不能大于剩余可投金额！");
//                            tv_tishi.setText(");
                            return;
                        }
                    }


                    if ((coupon_name.getText().toString().equals("未使用") && tishi_coupon)) {
                        tishi_coupon = false;
                        li_tishi.setVisibility(View.VISIBLE);
//                        tv_tishi.setText("亲，您还有未使用的福利哦!");
                        if (addAndSubView.getNum() != 0 && addAndSubView.getNum() > Double.valueOf(tv_availableAmount.getText().toString()).intValue()) {
//                                                tv_tishi.setText("");
                            Toast.makeText(V6iCoupActivity.this, "账户可用余额不足,请充值", Toast.LENGTH_SHORT).show();
                            tv_tishi.setText("账户可用余额不足,请充值!");
                            tishi_coupon = true;
                            return;
                        } else if (addAndSubView.getNum() < minAmount) {
                            Toast.makeText(V6iCoupActivity.this, "投资金额不能小于最低起投金额", Toast.LENGTH_SHORT).show();
                            tv_tishi.setText("投资金额不能小于最低起投金额");
                            tishi_coupon = true;
                            return;
                        } else if (addAndSubView.getNum() > maxAmount) {

                            ToastUtil.showCenterToast(V6iCoupActivity.this,
                                    "投资金额不能大于" + maxAmount.intValue() + "!");
                            tv_tishi.setText("投资金额不能大于" + maxAmount.intValue() + "!");
                            tishi_coupon = true;
                            return;
                        } else if ((addAndSubView.getNum() - minAmount)
                                % increaseNum != 0) {
                            li_tishi.setVisibility(View.VISIBLE);
                            tv_tishi.setText("投资金额的增量要为" + increaseNum + "的倍数");
                            ToastUtil.showCenterToast(V6iCoupActivity.this,
                                    "投资金额的增量要为" + increaseNum + "的倍数");
                            tishi_coupon = true;
                            return;
                        } else {
                            tv_tishi.setText("亲，您还有未使用的福利哦!");
                        }
                        changeStatus();
//						Toast.makeText(V6investActivity.this, "亲，您还有未使用的礼券哦!", 2000).show();
                        return;
                    }
//


                    if (resultcoupon) {

                        if (addAndSubView.getNum() < Integer.valueOf(touzijine)) {
//                            Toast.makeText(V6iCoupActivity.this, "不满足抵扣券的起投金额！", Toast.LENGTH_SHORT).show();

                            ToastUtil.showCenterToast(V6iCoupActivity.this,
                                    "不满足抵扣券的起投金额！");
                        } else {
                            flagcoupon();
                        }


                    } else {
                        if (addAndSubView.getNum() != 0 && addAndSubView.getNum() > Double.valueOf(tv_availableAmount.getText().toString()).intValue()) {
//                                                tv_tishi.setText("");

                            ToastUtil.showCenterToast(V6iCoupActivity.this,
                                    "账户可用余额不足,请充值");
                            tv_tishi.setText("账户可用余额不足,请充值!");
                            tishi_coupon = true;
                            return;
                        } else if ((addAndSubView.getNum() - minAmount)
                                % increaseNum != 0) {
                            tishi_coupon = true;
                            li_tishi.setVisibility(View.VISIBLE);
                            tv_tishi.setText("投资金额的增量要为" + increaseNum + "的倍数");
                            ToastUtil.showCenterToast(V6iCoupActivity.this,
                                    "投资金额的增量要为" + increaseNum + "的倍数");
                            return;
                        } else if (coupon_name.getText().toString().equals("未使用") && tishi_coupon) {
                            tishi_coupon = false;
                            li_tishi.setVisibility(View.VISIBLE);
                            tv_tishi.setText("亲，您还有未使用的福利哦!");
                            return;
                        } else {

//                            intent = new Intent(V6iCoupActivity.this, PayActivity2.class);
                            intent = new Intent(V6iCoupActivity.this, PayActivity.class);
                            intent.putExtra("flagRedpack", flagred);
                            intent.putExtra("rate", rate);
                            intent.putExtra("qixian", month);
                            intent.putExtra("minAmount", minAmount);

                            intent.putExtra("isZhi", 0);
                            if (json.optBoolean("payPass")) {
                                intent.putExtra("setPass", 0);
                            } else {
                                intent.putExtra("setPass", 1);
                                intent.putExtra("openfingerPay", 1);
                            }
                            intent.putExtra("flagFresh", flagFresh);
                            intent.putExtra("productId", loanid);
                            intent.putExtra("title_tpye", type);
                            intent.putExtra("increaseNum", increaseNum);
                            intent.putExtra("remainAmount", remainAmount);
                            intent.putExtra("myflag", myflag);

//                        getIntentmoney
                            try {
                                intentpaymoney = addAndSubView.getNum() - Integer.valueOf(getIntentmoney);
                            } catch (Exception e) {

                            }

                            if (myflag.equals("1")) {
                                intent.putExtra("red_id", reward_id);
                            }

                            if (myflag.equals("2")) {
                                intent.putExtra("money", shijie + "");
                                intent.putExtra("cardId", cardId);
                            } else {
                                intent.putExtra("raisingratesId", reward_id);
                                intent.putExtra("money", addAndSubView.getNum() + "");
                            }


                            intent.putExtra("flagcoupon", resultcoupon);
                            intent.putExtra("useCurrentAmountToInvest", ishuo);
                            startActivityForResult(intent, 0);
                            overridePendingTransition(R.anim.slide_in_from_bottom,
                                    R.anim.slide_out_to_bottom);
                        }


                    }

                    System.out.println("productdetails=rate===" + rate);
//					if (!json.optBoolean("payPass")) {
//						intent.putExtra("setPass", 1);
//					}


                } else {
                    if (jsonObject.optString("comment").equals("please login")) {


                        Intent intent = new Intent(V6iCoupActivity.this,
                                V6LoginActivity.class);
                        intent.putExtra("close", 1);
                        startActivity(intent);


                    } else {
                        ToastUtil.showCenterToast(V6iCoupActivity.this,
                                jsonObject.optString("comment"));
                    }
                }
            }

            @Override
            public void onError() {
                // TODO Auto-generated method stub

            }
        };
        vollery.setHeaders("token", getToken());
        vollery.setHeaders("client", "android");
        vollery.setShow(true);
        vollery.setRequestMethod(0);
        vollery.execute(V6iCoupActivity.this, Constant.URL
                + "/api/v5/user/userInfo", "");
    }

    private void incoupon() {
        if (!red_type.equals("")) {

            DecimalFormat df = new DecimalFormat(
                    "######0.00");
            double a = VoteCalculator
                    .getVoteCalculator_month_Dou(Double.valueOf(addAndSubView.getNum()), coupon_rate,
                            month);
//			SpannableString titleString = new SpannableString("收益:"+df.format(a)+"元");
//			titleString.setSpan(new ForegroundColorSpan(Color.parseColor("#f0f0f0")), 0, 3, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            coupon_num.setText(Regular.setSpannablecolor("收益:" + df.format(a) + "元", "#cccccc", 0, 3));
     /*       shijie=String.valueOf(Integer.valueOf(addAndSubView.getNum()-Integer.valueOf(shijie)));
            text_shiji.setText(shijie);*/


        }
    }

    /*
    * 实际多少元
    * */
    private void product_ShouYi() {
        DecimalFormat df = new DecimalFormat(
                "######0.00");
        double a = VoteCalculator
                .getVoteCalculator_month_Dou(Double.valueOf(addAndSubView.getNum()), rate,
                        month);
        tv_product_shouyi.setText(df.format(a) + "元");

   /*     int b = addAndSubView.getNum();
        int c = Integer.valueOf(text_shiji.getText().toString());
        int d = b - c;*/
        int b = addAndSubView.getNum();
        int c = Integer.valueOf(dikou);
        int d = b - c;
        if (d <= 0) {
            text_shiji.setText("0");
        } else {
            text_shiji.setText(String.valueOf(d));
        }

        if (b < coupon_max) {
            text_coupon.setVisibility(View.GONE);
            layout_dikou.setVisibility(View.GONE);

        } else {

        }


    }

    String flag_zhiwen = "1";

    public void onFingerprintClick(final int type) {

        FingerprintUtil.callFingerPrint(new FingerprintUtil.OnCallBackListenr() {

            @Override
            public void onSupportFailed() {
                showToast("当前设备不支持指纹");
            }

            @Override
            public void onInsecurity() {
                showToast("当前设备未处于安全保护中");
            }

            @Override
            public void onEnrollFailed() {
                showToast("请到设置中设置指纹");
            }

            @Override
            public void onAuthenticationStart() {
//                li_tishi.getVisibility()

                builder = new CustomDialog_view.Builder(V6iCoupActivity.this);
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        FingerprintUtil.cancel();
                        builder.create().dismiss();
                    }
                });
                builder.setMessage("“优点智投”的 Touch ID");
                builder.setMessage2("请验证已有手机指纹");
                builder.create().setCancelable(false);
                builder.create().show();


            }

            @Override
            public void onAuthenticationError(int errMsgId, CharSequence errString) {
                if (errMsgId == 7) {
                    verityUser();
                } else {

                    showToast(errString.toString());
                }
                if (builder.create() != null) {
                    builder.create().dismiss();
                }


            }

            @Override
            public void onAuthenticationFailed() {
                showToast("再试一次");
            }

            @Override
            public void onAuthenticationHelp(int helpMsgId, CharSequence helpString) {
                showToast(helpString.toString());
            }

            @Override
            public void onAuthenticationSucceeded(FingerprintManagerCompat.AuthenticationResult result) {
//                showToast("解锁成功");
                if (builder.create() != null) {
                    builder.create().dismiss();
                    if (type == 1) {

                        invest();

                    }
                } else {

                }
            }


        });
    }

    /**
     * 指纹投资
     */
    private void invest() {
        AjaxParams params = new AjaxParams();

        params.put("productType", type);
        params.put("productId", loanid);
//        params.put("amount", addAndSubView.getNum() + "");
//        intent.putExtra("money", shijie + "");


        params.put("payType", "FINGERPRINT");
        params.put("useCurrentAmountToInvest", ishuo + "");
        if (myflag.equals("1")) {
            params.put("amount", addAndSubView.getNum() + "");
            params.put("giftId", reward_id);
        }

        if (myflag.equals("2")) {
            if (addAndSubView.getNum() < coupon_max) {
                params.put("amount", addAndSubView.getNum() + "");
            } else {
//                params.put("amount", shijie + "");

                int b = Integer.valueOf(dikou);
                int c = addAndSubView.getNum() - b;
                params.put("amount", c + "");

            }


            params.put("cardId", cardId);

        } else if (myflag.equals("3")) {
            params.put("amount", addAndSubView.getNum() + "");
            params.put("raisingratesId", reward_id);
        } else {
            params.put("cardId", cardId);
        }
     /*   if (flagred) {
            params.put("giftId", reward_id);
        }


        if (resultcoupon) {

            params.put("raisingratesId", reward_id);
        }*/
        Log.e("投资", params.toString());


        VolleryUtil vollery = new VolleryUtil() {

            @Override
            public void onUpDate(final JSONObject jsonObject) {
                // TODO Auto-generated method stub
                Log.e("invest", jsonObject.toString());
                if (jsonObject.optInt("success") == 1) {
                    clearCoupon();

                    Log.e("invest", jsonObject.toString());
                    ToastUtil.showCenterToast(V6iCoupActivity.this,
                            jsonObject.optString("comment"));
                    final JSONObject json1 = jsonObject.optJSONObject("data");

                    firstType = json1.optString("firstinvestType");
                    firstamount = json1.optString("ExperienceAmount");
                    if (json1.optBoolean("firstFlag")) {
                        firstType = "1";
                    } else {
                        firstType = "0";
                    }
//								
//								if(MainActivity.instance!=null){
//									MainActivity.instance.finish();
//								}
                    Intent intent = new Intent(V6iCoupActivity.this, MainActivity.class);
                    intent.putExtra("submit", json1.optString("expectDueDate"));
                    intent.putExtra("amount", addAndSubView.getNum() + "");
                    intent.putExtra("huo", 1);
                    intent.putExtra("url", "?isInvest=1&flagFresh=" + flagFresh + "&isFirstInvest=" + firstType + "&productType=" + "dq" + "&amount=" + addAndSubView.getNum() + "&action=1");
                    intent.putExtra("firstamount", firstamount);
                    intent.putExtra("firstType", firstType);
                    intent.putExtra("invest", 1);
                    intent.putExtra("useCurrentAmountToInvest", ishuo);
                    intent.putExtra("useCurrAmount", json1.optDouble("useCurrAmount"));
                    intent.putExtra("useFundAmount", json1.optDouble("useFundAmount"));
                    intent.putExtra("rate", json1.optString("count_rate_date"));
//								startActivityForResult(intent,1);
                    Editor editor = getShare().edit();
                    if (firstType.equals("1")) {
                        editor.putInt("firstType", 1);
                        editor.putString("active_url", "?isInvest=1&flagFresh=" + flagFresh + "&isFirstInvest=" + firstType + "&productType=" + "dq" + "&amount=" + addAndSubView.getNum() + "&action=1");
                    } else {
                        editor.putInt("firstType", 0);
                    }
                    editor.commit();
                    setResult(5, intent);
                    finish();
//							}


                } else {
                    ToastUtil.showCenterToast(V6iCoupActivity.this,
                            jsonObject.optString("comment"));

                    if (jsonObject.optString("comment").equals(
                            "please login")) {
                        if (jsonObject.optString("comment").equals("please login")) {
                            Intent intent = new Intent(V6iCoupActivity.this,
                                    LoginActivity.class);
                            intent.putExtra("close", 1);
                            startActivity(intent);
                        }
                    }


                }
            }

            @Override
            public void onError() {
                // TODO Auto-generated method stub
                Intent intent = new Intent(V6iCoupActivity.this, V6ResultFinsh.class);
                intent.putExtra("title", "投资结果");

                intent.putExtra("description1", "您好，系统正在处理中，\n请3~5分钟之后前往 “我的账户-定期计划”");

                intent.putExtra("description2", "确认投资结果！若投资失败请重新操作");
                intent.putExtra("button", "我的账户");
                startActivity(intent);


            }
        };


        vollery.setShow(true);
        vollery.setRequestMethod(1);
        vollery.setHeaders("token", getToken());
        vollery.setHeaders("client", "android");
        vollery.setHeaders("deviceId", getdeviceId());
        vollery.setHeaders("deviceToken", getdevice_token());
        vollery.execute(this, Constant.URL
                + "api/v6/invest/invest?", params.toString());


    }

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        Log.e("删除键", "destroy");
        if (sha != null) {
            Editor editor = sha.edit();
            editor.clear();
            editor.commit();
        }
        Log.e("ondestroy", "ondestroy");
    }

    public void flagcoupon() {
        AjaxParams params = new AjaxParams();
        params.put("productId", loanid);
        //金额传实际支付的

        params.put("amount", addAndSubView.getNum() + "");
        params.put("raisingratesId", reward_id);
        params.put("type", type);
        params.put("useCurrentAmountToInvest", ishuo + "");
        VolleryUtil volleryUtil = new VolleryUtil() {

            @Override
            public void onUpDate(JSONObject jsonObject) {
                // TODO Auto-generated method stub
                if (jsonObject.optInt("success") == 1) {
                    LogUtils.e("加息券校验", jsonObject.toString());

                    Log.d(jsonObject.toString(), "444444444444");


                    if (addAndSubView.getNum() != 0 && addAndSubView.getNum() > Double.valueOf(tv_availableAmount.getText().toString()).intValue()) {
//                                                tv_tishi.setText("");
                        Toast.makeText(V6iCoupActivity.this, "账户可用余额不足,请充值!", Toast.LENGTH_SHORT).show();
                        tv_tishi.setText("账户可用余额不足,请充值!");
                        tishi_coupon = false;
                        return;
                    } else if (addAndSubView.getNum() < minAmount) {
                        ToastUtil.showCenterToast(V6iCoupActivity.this,
                                "投资金额不能小于最低起投金额");
                        tv_tishi.setText("投资金额不能小于最低起投金额");
                        tishi_coupon = true;
                        return;
                    } else if (addAndSubView.getNum() > maxAmount) {

                        ToastUtil.showCenterToast(V6iCoupActivity.this,
                                "投资金额不能大于" + maxAmount.intValue() + "!");
                        tv_tishi.setText("投资金额不能大于" + maxAmount.intValue() + "!");
                        tishi_coupon = true;
                        return;
                    } else if ((addAndSubView.getNum() - minAmount)
                            % increaseNum != 0) {
                        li_tishi.setVisibility(View.VISIBLE);
                        tv_tishi.setText("投资金额的增量要为" + increaseNum + "的倍数");
                        ToastUtil.showCenterToast(V6iCoupActivity.this,
                                "投资金额的增量要为" + increaseNum + "的倍数");
                        tishi_coupon = true;
                        return;
                    } else if (coupon_name.getText().toString().equals("未使用") && tishi_coupon) {
                        tishi_coupon = false;
                        li_tishi.setVisibility(View.VISIBLE);
                        tv_tishi.setText("亲，您还有未使用的福利哦!");
                        return;
                    } else {

//                        Intent intent = new Intent(V6iCoupActivity.this, PayActivity2.class);
                        Intent intent = new Intent(V6iCoupActivity.this, PayActivity.class);
//					intent.putExtra("flagRedpack", flagred);
                        intent.putExtra("rate", rate);
                        intent.putExtra("qixian", month);
                        intent.putExtra("minAmount", minAmount);
                        intent.putExtra("isZhi", 0);
                        intent.putExtra("cardId", cardId);
                        intent.putExtra("flagFresh", flagFresh);
                        intent.putExtra("productId", loanid);
                        intent.putExtra("title_tpye", type);
                        intent.putExtra("increaseNum", increaseNum);
                        intent.putExtra("remainAmount", remainAmount);
                        intent.putExtra("myflag", myflag);

//                    intent.putExtra("money", addAndSubView.getNum() + "");
                        try {
                            int intentpaymoney = addAndSubView.getNum() - Integer.valueOf(getIntentmoney);
                            if (myflag.equals("2")) {
                                intent.putExtra("money", intentpaymoney + "");
                            } else {
                                intent.putExtra("money", addAndSubView.getNum() + "");
                                layout_liquan.setVisibility(View.GONE);
                                text_title.setVisibility(View.GONE);
                                layout_dikou.setVisibility(View.GONE);
                            }

                        } catch (Exception e) {

                        }


//                    intent.putExtra("money", addAndSubView.getNum() + "");
                        if (myflag.equals("1")) {
                            intent.putExtra("red_id", reward_id);
                        }

                        if (myflag.equals("2")) {
                            intent.putExtra("money", shijie + "");
                            intent.putExtra("cardId", cardId);
                        } else {
                            intent.putExtra("raisingratesId", reward_id);
                            intent.putExtra("money", addAndSubView.getNum() + "");
                        }


                        intent.putExtra("flagcoupon", resultcoupon);
                        intent.putExtra("raisingratesId", reward_id);
                        intent.putExtra("useCurrentAmountToInvest", ishuo);
                        startActivityForResult(intent, 1);
                        overridePendingTransition(R.anim.slide_in_from_bottom,
                                R.anim.slide_out_to_bottom);
                    }


                } else {
                    if (jsonObject.optString("comment").equals("请先设置支付密码")) {
                        if (addAndSubView.getNum() != 0 && addAndSubView.getNum() > Double.valueOf(tv_availableAmount.getText().toString()).intValue()) {
//                                                tv_tishi.setText("");

                            ToastUtil.showCenterToast(V6iCoupActivity.this,
                                    "账户可用余额不足,请充值");
                            tv_tishi.setText("账户可用余额不足,请充值!");
                            tishi_coupon = false;
                        } else if (addAndSubView.getNum() < minAmount) {
                            ToastUtil.showCenterToast(V6iCoupActivity.this,
                                    "投资金额不能小于最低起投金额");
                            tv_tishi.setText("投资金额不能小于最低起投金额");
                            tishi_coupon = false;
                            return;
                        } else if (addAndSubView.getNum() > maxAmount) {

                            ToastUtil.showCenterToast(V6iCoupActivity.this,
                                    "投资金额不能大于" + maxAmount.intValue() + "!");
                            tv_tishi.setText("投资金额不能大于" + maxAmount.intValue() + "!");
                            tishi_coupon = false;
                            return;
                        } else if ((addAndSubView.getNum() - minAmount)
                                % increaseNum != 0) {
                            li_tishi.setVisibility(View.VISIBLE);
                            tv_tishi.setText("投资金额的增量要为" + increaseNum + "的倍数");
                            ToastUtil.showCenterToast(V6iCoupActivity.this,
                                    "投资金额的增量要为" + increaseNum + "的倍数");
                            tishi_coupon = false;
                        }  else {

                            Intent intent = new Intent(V6iCoupActivity.this, PayActivity.class);
//						intent.putExtra("flagRedpack", flagred);
                            intent.putExtra("rate", rate);
                            intent.putExtra("qixian", month);
                            intent.putExtra("minAmount", minAmount);
                            intent.putExtra("isZhi", 0);
                            intent.putExtra("setPass", 1);
                            intent.putExtra("openfingerPay", 1);
                            intent.putExtra("flagFresh", flagFresh);
                            intent.putExtra("productId", loanid);
                            intent.putExtra("title_tpye", type);
                            intent.putExtra("increaseNum", increaseNum);
                            intent.putExtra("remainAmount", remainAmount);
                            intent.putExtra("myflag", myflag);
//                            intent.putExtra("cardId", cardId);


//                        intent.putExtra("money", addAndSubView.getNum() + "");
                            int intentpaymoney = addAndSubView.getNum() - Integer.valueOf(getIntentmoney);
                            if (myflag.equals("2")) {
                                intent.putExtra("money", intentpaymoney + "");
//                            intent.putExtra("money", addAndSubView.getNum() + "");
                            } else {
                                intent.putExtra("money", addAndSubView.getNum() + "");
                            }
                            if (myflag.equals("1")) {
                                intent.putExtra("red_id", reward_id);
                            }

                            if (myflag.equals("2")) {
                                intent.putExtra("money", shijie + "");
                                intent.putExtra("cardId", cardId);
                            } else {
                                intent.putExtra("raisingratesId", reward_id);
                                intent.putExtra("money", addAndSubView.getNum() + "");
                            }
                            intent.putExtra("flagcoupon", resultcoupon);
                            intent.putExtra("raisingratesId", reward_id);
                            intent.putExtra("useCurrentAmountToInvest", ishuo);
                            startActivityForResult(intent, 1);
                            overridePendingTransition(R.anim.slide_in_from_bottom,
                                    R.anim.slide_out_to_bottom);

                        }


                    }
                    Toast.makeText(V6iCoupActivity.this, jsonObject.optString("comment"), 2000).show();
                }
            }

            @Override
            public void onError() {
                // TODO Auto-generated method stub

            }
        };
        volleryUtil.setHeaders("token", getToken());
        volleryUtil.setHeaders("deviceId", getdeviceId());
        volleryUtil.setRequestMethod(1);
        volleryUtil.setShow(true);
        volleryUtil.execute(V6iCoupActivity.this, Constant.URL + "/api/v6/invest/invests/valid?", params.toString());
    }

    private void showDialog() {
        mDialog = new CustomDialog.Builder(V6iCoupActivity.this);

        mDialog.setMessage("优点智投上线指纹支付功能，是否开启使用？");
        mDialog.create().setCancelable(false);
        mDialog.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub

                mDialog.create().dismiss();
                Editor editor2 = getShare().edit();
                editor2.putInt("invest_num", getInvestNum() + 1);
                editor2.commit();
            }
        });
        mDialog.setPositiveButton(R.string.confirm, new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                if (!keyguardManager.isKeyguardSecure()) {
                    mDialog.create().dismiss();
                    FingerSetting.openFingerPrintSettingPage(V6iCoupActivity.this);

                    return;
                }
                mStartActivity(V6iCoupActivity.this, V6FingerPayPwdConfirmActivity.class);
                mDialog.create().dismiss();

            }
        });
        mDialog.create().show();
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        Intent intent = null;
        switch (v.getId()) {
            case R.id.rl_back:
                Log.e("删除键", "rl_back");
                if (sha != null) {
                    Editor editor = sha.edit();
                    editor.clear();
                    editor.commit();
                }
                finish();
                break;
            case R.id.rl_use_coupon:
                if (coupon_name.getText().toString().equals("登录查看")) {
                    intent = new Intent(V6iCoupActivity.this,
                            V6LoginActivity.class);
                    intent.putExtra("close", 1);
                    startActivity(intent);
                    return;
                }
                intent = new Intent(V6iCoupActivity.this,
                        Investcoupon.class);
//			intent3.putExtra("rate", rate);
//			intent3.putExtra("month", month);
//			intent3.putExtra("money", et_money.getText().toString());
//			intent3.putExtra("red", availableGiftAmount);
                intent.putExtra("isnew", false + "");
                intent.putExtra("isred", false + "");
                intent.putExtra("id", loanid);
                intent.putExtra("flag", false + "");
                intent.putExtra("type", type);
                intent.putExtra("amount", addAndSubView.getNum());
                startActivityForResult(intent, 0);
                break;
            case R.id.tv_add_money:
                intent = new Intent(V6iCoupActivity.this,
                        V6WebviewChild.class);
                intent.putExtra("url", Constant.URL + "h5/account/recharge/view?app=true&token=");
                intent.putExtra("title", "智投宝");
                startActivity(intent);
                break;
            case R.id.btn_invest:
                if (coupon_name.getText().toString().equals("登录查看")) {
                    intent = new Intent(V6iCoupActivity.this,
                            V6LoginActivity.class);
                    intent.putExtra("close", 1);
                    intent.putExtra("web", 1);
                    startActivityForResult(intent, 1);
                    return;
                }
                if (flagFresh.equals("true")) {
                    if (getisNewUser().equals("false")) {
                        li_tishi.setVisibility(View.VISIBLE);
                        tv_tishi.setText("该项目仅限新手投资");
                        return;
                    }

                }
                if (getisFinger() == 1) {
                    checkUserInfo();
                } else {
                    verityUser();
                }

             /*   try {
                    if (addAndSubView.getNum() < coupon_max && myflag.equals("2")) {
//                    Toast.makeText(this, "", Toast.LENGTH_SHORT).show();
                    } else {


                    }
                } catch (Exception e) {

                }*/

//			if(getInvestNum()==0&getisFinger()==1&getisFingerPay()==0){
//				showDialog();
//			
//				return;
//			}


                break;
            case R.id.tv_mAgreement:
               /* intent = new Intent(V6iCoupActivity.this,
                        V4WebViewActivity.class);*/
                intent = new Intent(V6iCoupActivity.this, V4WebViewRegisterActivity2.class);

                intent.putExtra("url","/landing/mAgreement?app=true&appversion=true&productId=" + loanid);
                intent.putExtra("title", "认购协议");
                intent.putExtra("share", 1);
                startActivity(intent);
                break;
            case R.id.rl_dexterity:
                if (check_dexterity.isChecked()) {
                    img_ck_status.setVisibility(View.GONE);
                    check_dexterity.setChecked(false);
                } else {
                    img_ck_status.setVisibility(View.VISIBLE);
                    check_dexterity.setChecked(true);
                }
                ishuo = check_dexterity.isChecked();
                myconmment();
                break;
            case R.id.check_mAgreement:
                if (check.isChecked()) {
                    btn_invest.setEnabled(false);
                    btn_invest.setBackgroundResource(R.drawable.btn_unclick);
                    check.setChecked(false);
                } else {
                    btn_invest.setEnabled(true);
                    btn_invest.setBackgroundResource(R.drawable.v6button);
                    check.setChecked(true);
                }
                break;
        }
    }

    @Override
    public void onConfirm() {
        // TODO Auto-generated method stub
        Intent intent = null;
        switch (mAlertDialog.getType()) {

            case "bindcard":
                intent = new Intent(V6iCoupActivity.this,
                        V6WebviewChild.class);
                intent.putExtra("url", Constant.URL + "h5/account/bankcard/bind/view?app=true&token=");
                intent.putExtra("title", "智投宝");
                startActivity(intent);
                break;
            case "password":
                intent = new Intent(V6iCoupActivity.this,
                        V6WebviewChild.class);
                intent.putExtra("url", Constant.URL + "h5/setTraPwd?app=true&token=");
                intent.putExtra("title", "智投宝");
                startActivity(intent);
                break;
            case "ceping":
                Intent intent0 = null;

                intent0 = new Intent(V6iCoupActivity.this, WebViewActivity.class);
                intent0.putExtra("url", "h5/assess/answer" + "?app=true&token=");
                // intent0.putExtra("url",
                // "h5/assess/index?showHeader=1"+"?app=true&token=");
                // intent0.putExtra("flag", 1);
                // intent2.putExtra("url",
                // "/h5/messageCenter?app=true&token="+getToken());
                intent0.putExtra("share", 1);
                intent0.putExtra("titleview", 1);
                intent0.putExtra("title", "风险测评");
                startActivity(intent0);
                break;
        }
    }

    @Override
    public void onCancle() {
        // TODO Auto-generated method stub

    }

}
